﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL.Util;
using DTO;
using BLL;

namespace MZ.Money
{
    public partial class Bankbook : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCard();
            }
        }

        /// <summary>
        /// 绑定消费账户
        /// </summary>
        private void BindCard()
        {
            DdlMyCard.DataSource = CacheHelper.GetMyCard(User.Identity.Name);
            DdlMyCard.DataBind();

            DdlRate.DataSource = CacheHelper.GetRate();
            DdlRate.DataBind();
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            var rate = Config.GetRateByID(int.Parse(DdlRate.SelectedValue));
            BankBook bb = new BankBook()
            {
                AID = int.Parse(User.Identity.Name),
                Capital = decimal.Parse(TxtMoney.Text),
                Card = CardBLL.GetMyCard(User.Identity.Name, DdlMyCard.SelectedValue),
                CardID = int.Parse(DdlMyCard.SelectedValue),
                Cycle = rate.Months,
                Rate = float.Parse(TxtRate.Text) / 100,
                SaveTime = DateTime.Parse(TxtRecordTime.Text)
            };

            bb.Total = Convert.ToDecimal(Convert.ToDouble(bb.Capital) * bb.Rate / 12 * bb.Cycle) + bb.Capital;
            bb.Total = decimal.Round(bb.Total, 2) == bb.Total ? bb.Total : bb.Total + decimal.Parse("0.01");
            BankbookBLL.AddBankBook(bb);
        }



    }
}
